<?php


namespace app\cms\community\lib\utils\addons;


use app\cms\community\lib\utils\addons\exception\addonsException;
use app\cms\community\lib\utils\addons\lib\saveAddonsAbstract;
use iflow\Utils\basicTools;

class downloadAddons extends saveAddonsAbstract
{
    /**
     * gitClone 插件
     * @param string $git
     * @return $this
     * @throws addonsException
     */
    public function gitCloneAddons(string $git): static
    {
        $fileName = basename($git);
        $path = $this->addonsRootPath . DIRECTORY_SEPARATOR . $fileName;

        if (is_dir($path)) {
            throw new addonsException('当前插件目录已存在', 401);
        }

        mkdir($path);

        // 执行Shell 下载git远程仓库代码
        $shell = (new basicTools()) -> execShell("cd {$this -> addonsRootPath} && git clone ${git}");
        if (!is_dir($path)) {
            rmdir($path);
            throw new addonsException('下载远程仓库代码失败'. $shell, 401);
        }

        $this->addonsRootPath = $path;
        $this->tempPath = $path;
        return $this;
    }
}